function connectWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(window.WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
     setTimeout(function () { document.documentElement.removeChild(WVJBIframe) }, 0)
}
//android传参需要转成字符串

    var HTBridge ;
    HTBridge = {
        isIos:!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
        inApp:window.navigator.userAgent.indexOf("/ehtsec") != -1 || window.WebViewJavascriptBridge || window.WVJBCallbacks,
        initBridge:function (callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('initBridge',callBackFunction);
                });
            }
        },
        //保存图片到相册
        saveImageToAlbum:function(params){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('ht_square_function_saveImageToAlbum',params);
                });
            }else{
                if(window.appData){
                    window.appData.downloadImage(params)
                }
            }
        },
        htShareBase64Image:function(params){
			var _this=this
			if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.callHandler('htShareAction', params);
				});
			}else{
                if(window.appData){
                    window.appData.shareImage(params.base64, params.shareToWx, params.shareToTimeLine)
                }
            }
        },

        //获取行情数据（返回带key） 9.0
        htQueryQuoteList:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htQueryQuoteList',params,callBackFunction);
                });
            }else{
                window.quote.requestQuote(params);
                window.updateLinster = function(data){
                    callBackFunction(data)
                };
            }
        },

        //页面出现 8.97
        viewWillAppear:function(callBackFunction){
			var _this=this
            if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.registerHandler('viewWillAppear',callBackFunction);
				});
			}else{
                window.viewWillAppear = function(){
                    callBackFunction()
                }
            }
        },
        //页面消失 8.97
        viewWillDisappear:function(callBackFunction){
			var _this=this
            if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.registerHandler('viewWillDisappear',callBackFunction);
				});
			}else{
                window.viewWillDisappear = function(){
                    callBackFunction()
                }
            }
        },

        //退出社区登录 8.97
        doCommunityLogout:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('doCommunityLogout',callBackFunction);
                });
            }else{
                if(window.squareOuterObj){
                    window.squareOuterObj.doCommunityLogout()
                }
            }
        },
        //添加足迹 8.97
        doAddFootPrint:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('doAddFootPrint',params,callBackFunction);
                });
            }else{
                if(window.appData){
                    if(window && window.appData && window.appData.doAddFootPrint){
                        window.appData.doAddFootPrint(JSON.stringify(params));
                    }
                }else{
                    console.error("当前webView未注入社区模块")
                }
            }
        },
        //获取网络状态
        getNetworkType:function(callBackFunction){//8.96版本可用
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getNetworkType',callBackFunction);
                });
            }else{
                if(window.squareDataObj){
                    let obj = window.squareDataObj.getNetworkType()
                    var response = JSON.parse(obj)
                    callBackFunction(response)
                }
            }
        },
        //评论
        doComment:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('community_doComment',params,callBackFunction);
                });
            }else{
                if(window.squareDataObj){
                    if(window && window.squareDataObj && window.squareDataObj.doComment){
                        window.squareDataObj.doComment(JSON.stringify(params));
                    }
                    if(window && window.appData && window.appData.sendH5Info){
                        window.appData.sendH5Info(JSON.stringify({callJsWhenResume:params.callJsWhenResume}))
                    }
                }else{
                    console.error("当前webView未注入社区模块")
                }
            }
        },

        getOuterCommunityAccount:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getPartnerCommunityAccount',callBackFunction);
                });
            }else{
                if(window.squareOuterObj){
                    var response = JSON.parse(window.squareOuterObj.getData())
                    callBackFunction(response)
                }else{
                    console.error("当前webView未注入社区模块")
                }
            }

        },

        updateOuterCommunity:function(callBackFunction){
			var _this=this
            if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.registerHandler('updateCommunityToPartner',callBackFunction);
				});
			}else{
                window.updateOuterCommunity = function(){
                    if(window.squareOuterObj){
                        var response = JSON.parse(window.squareOuterObj.getData())
                        callBackFunction(response)
                    }
                 }
			}
        },

        //商城产品是否是自选股
        tkmall_isOptional:function (params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('tkmall_isOptional',params,callBackFunction);
                });
            }else{
                if(window.appData){
                    var str = JSON.stringify(params);
                    let obj = window.appData.tkmallIsOptional(str)
                    var response = JSON.parse(obj)
                    callBackFunction(response)
                }
            }
        },
        //商城产品添加自选股
        tkmall_addOptional:function (params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('tkmall_addOptional',params,callBackFunction);
                });
            }else{
                if(window.appData){
                    var str = JSON.stringify(params);
                    let obj = window.appData.tkmallAddOptional(str)
                    var response = JSON.parse(obj)
                    callBackFunction(response)
                }
            }
        },
        //商城产品打开商品详情页
        tkmall_openDetail:function (params){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('tkmall_openDetail',params);
                });
            }else{
                if(window.appData){
                    var str = JSON.stringify(params);
                    window.appData.tkmallOpenDetail(str)
                }
            }
        },

        getHTInformation:function (callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getHTInformation',callBackFunction);
                });
            }
        },
        getAppInfo:function (params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getAppInfo',params,callBackFunction);
                });
            }else{
                if(window.appData){
                    let obj = window.appData.getAppInfo(params)
                    callBackFunction(obj)
                }
            }
        },

        getAPPInformation:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getAPPInformation',callBackFunction);
                });
            }
        },
        getCommunityAccount:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('getCommunityAccount',callBackFunction);
                });
            }else{
                if(window.squareDataObj){
                    var response = {
                        token : squareDataObj.getToken(),
                        account: squareDataObj.getAccount(),
                        phoneNo:squareDataObj.getSquareHideMobile(),
			            nickName:squareDataObj.getSquareNickname(),
                        avatar_path:window.squareDataObj.getAvatar(),
                        // encrypt_token: window.squareDataObj.getEHTXEncryptToken(),
                        certificate:window.squareDataObj.getCertificate()
                    }
                    callBackFunction(response)
                }else{
                    console.error("当前webView未注入社区模块")
                }
            }

        },
        updateCommunity:function(callBackFunction){
			var _this=this
            if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.registerHandler('updateCommunity',callBackFunction);
				});
			}else{
                window.updateCommunity = function(){
                    if(window.squareDataObj){
                        var response = {
                            token : window.squareDataObj.getToken(),
                            account: window.squareDataObj.getAccount(),
                            phoneNo:window.squareDataObj.getSquareHideMobile(),
                            nickName:window.squareDataObj.getSquareNickname(),
                            avatar_path:window.squareDataObj.getAvatar(),
                            // encrypt_token: window.squareDataObj.getEHTXEncryptToken(),
                            certificate: window.squareDataObj.getCertificate()
                        }
                        callBackFunction(response)
                    }
                 }
			}
        },

        setWxCode:function(callBackFunction){
            var _this=this
            if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.registerHandler('setWxCode',callBackFunction);
				});
			}else{
			}
        },

        wxLogin:function(callBackFunction){
            var _this = this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('wxLogin', callBackFunction);
                    bridge.registerHandler('setWxCode',callBackFunction);
                });
            }else{
                if(window.squareLoginObj){
                    squareLoginObj.wxLogin();

                    window.setWxCode_android = function(code){
                        callBackFunction(code)
                    }
                }
            }
        },

        htGetURLForKey:function(params,callBackFunction){
            var _this = this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htGetURLForKey',params,callBackFunction);
                });
            }else{
                if(window.squareLoginObj){
                    squareLoginObj.htGetURLForKey(params);
                    window.htGetURLForKey = function(str){
                        callBackFunction(str)
                    }
                }
            }
        },

        communityLogin:function(callBackFunction,needGoBack){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('communityLogin', function(){});
                    bridge.registerHandler('updateCommunity',callBackFunction);
                });
            }else{
                if(window.squareLoginObj){

                    window.updateCommunity = function(){
                       if(window.squareDataObj){
                           var response = {
                               token : window.squareDataObj.getToken(),
                               account: window.squareDataObj.getAccount(),
                               phoneNo:window.squareDataObj.getSquareHideMobile(),
                               nickName:window.squareDataObj.getSquareNickname(),
                               avatar_path:window.squareDataObj.getAvatar(),
                            //    encrypt_token:window.squareDataObj.getEHTXEncryptToken(),
                               certificate:window.squareDataObj.getCertificate()
                           }
                           callBackFunction(response)
                       }
                    }
                    if(needGoBack){
                        if(window.squareDataObj&&window.squareDataObj.getToken()){

                        }else{
                            squareLoginObj.communityLoginWithFinish();
                        }

                    }else{
                        if(window.squareDataObj&&window.squareDataObj.getToken()){

                        }else{
                            squareLoginObj.communityLogin()
                        }


                    }
                }else{
                    console.error("当前webView未注入社区模块")
                }
            }

        },
        htChangeFavorStatus:function(status,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htChangeFavorStatus', callBackFunction);
                });
            }else{
                window.location.href = "ehtsec://collectionStatus="+status
            }
        },
        htGetFavorStatus:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htGetFavorStatus', callBackFunction);
                });
            }else{
                window.location.href = "ehtsec://collectionStatus=2"
                window.syncCollectionStatus = (status) => {callBackFunction(status) }
            }
        },
        htRefreshFavorStatus:function(callBackFunction){
            var _this=this
            if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.registerHandler('htRefreshFavorStatus', callBackFunction)
             });
            }
        },
        htRequestQuote:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htRequestQuote', params,callBackFunction);
                });
            }else{
                window.quote.requestQuote(params);
                window.updateLinster = function(data){
                    callBackFunction(data)
                };
            }
        },
        //添加自选股
        htOptionalAddStocks:function(params,success){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htOptionalAddStocks',  params,success);
                });
            }else{
                if(window.appData){
                    var str = JSON.stringify(params);
                    window.appData.htOptionalAddStocks(str)
                    window.htOptionalAddStocks = function(){
                        success()
                    }
                }
            }
        },
        //移除自选股
        htOptionalRemoveStocks:function(params,success){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htOptionalRemoveStocks',  params,success);
                });
            }else{
		window.appData.htOptionalRemoveStocks(params)
            }
        },
        //获取自选股
        //params:{@"groupKey":},没有groupKey，拉取本地当前选中分组的自选股
        //callback:{@"codes":stockString} 返回选中的分组和股票
        //stockString：SH600837，SH600837
        htOptionalGetStocks:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htOptionalGetStocks', params,callBackFunction);
                })
            }else{
                if(window.appData){
                    var groupKey = params["groupKey"]
                    let obj = window.appData.htOptionalGetStocks(Number(groupKey))
                    callBackFunction(JSON.parse(obj))
                }
            }
        },
        htOptionalGetTeams:function(callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htOptionalGetTeams',  callBackFunction);
                })
            }else{
                if(window.appData){
                    let obj = window.appData.htOptionalGetTeams()
                    callBackFunction(obj)
                }
            }
        },
        //获取全部自选股
        //callback:{@"codes":stockString} 返回股票
    htOptionalGetAll:function(callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('htOptionalGetAll',  callBackFunction);
            })
        }
    },
        //弹出自选分组选择框，用户主动选择分组后，返回分组和自选股
        //params:{@"groupKey":},没有groupKey，拉取本地当前选中分组的自选股
        //callback:{@"groupKey":optionalGroup.groupKey,@"codes":stockString} 返回选中的分组和股票
        //stockString：SH600837，SH600837
    htOptionalSelectGroup:function(params,callBackFunction){
            var _this=this
            if(_this.isIos){
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('htOptionalSelectGroup', params,callBackFunction);
                })
            }
        },
        updateShareData:function(params){
			var _this=this
			if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.callHandler('updateShareData', params);
				});
			}else{
				window.gotoShare = function(){
                    if(window.shareObj){
                        window.shareObj.setShareData(JSON.stringify({
                            "title": params.title||'',
                            "summary": params.content||'',
                            "source":  params.source||'',
                            "linkUrl":  params.link||'',
                            "time": params.date||'',
                            "coverUrl":  params.imgUrl||'',
                            "businessType": params.businessType||'',
                            "newsId": params.newsId||''
                        }));
                    }

				}
                if (window.shareObj && window.shareObj.updateShareData) {
                    window.shareObj.updateShareData(JSON.stringify({
                        "title": params.title||'',
                        "summary": params.content||'',
                        "source":  params.source||'',
                        "linkUrl":  params.link||'',
                        "time": params.date||'',
                        "coverUrl":  params.imgUrl||'',
                        "businessType": params.businessType||'',
                        "newsId": params.newsId||''
                    }));
                }
			}

        },



        htShareAction:function(params){
			var _this=this
			if(_this.isIos){
				connectWebViewJavascriptBridge(function (bridge) {
					bridge.callHandler('htShareAction', params);
				});
			}else{
                if(window.shareObj){
                    window.shareObj.setShareData(
                        JSON.stringify({
                            "title": params.title||'',
                            "summary": params.content||'',
                            "source":  params.source||'',
                            "linkUrl":  params.link||'',
                            "time": params.date||'',
                            "coverUrl":  params.imgUrl||'',
                            "style":params.style||'',
                            "file":params.file||'',
                            "filename":params.filename||'',
                            "businessType":params.businessType||'',
                            "newsId":params.newsId||'',
                            "shareOptions":params.shareOptions||'',
                            "contentType":params.contentType||''
                        })
                    );
                }
			}
        },
        ht_log:function(event,properties){
            var _this=this
            if(_this.isIos){
                var params={
                    event:event,
                    properties:properties
                }
                connectWebViewJavascriptBridge(function (bridge) {
                    bridge.callHandler('ht_log', params);
                });
            }else{
                window.appData.statSAEvent(event,JSON.stringify(properties))
            }
        },
        ht_routeFunction(id,param,getPath){
            var _this=this
            var path = ''
            var encodeParam = ''
            var iosPramas = ''
            if(!id){
                console.error("url不正确")
            }
            if(param){
                var str = __$paramToUrlEncodeStr(param)
                encodeParam =  "&params="+encodeURIComponent(str)
                iosPramas = '&'+str
            }
            if(_this.isIos){
               path = "ehtsec://func?id="+id+iosPramas
            }else{
               if(_this.inApp){
                   path = "ehtsec://funid="+id+iosPramas
               }else{
                   path = "ehtsec://func?id="+id+ encodeParam
               }
            }
            if(getPath){
                return path
            }else{
                window.location.href = path
            }


        },
        ht_routeURL(url,param,getPath){
           if(!url){
               console.error("url不正确")
           }
           param.url = url
           var data = param || {}
           var _this=this,path=""
           data.url = url
           var str = __$paramToUrlEncodeStr(data)
           if(_this.isIos){
                path = "ehtsec://func?id=10161&"+str
            }else{
                path = "ehtsec://funid=10161&"+str
            }
        //    var path = "ehtsec://func?id=10161"+"&params="+encodeURIComponent(str)
           if(getPath){
            return path
            }else{
                window.location.href = path
            }
        },
        ht_OpenApp(url,param,getPath){
            if(!url){
                console.error("url不正确")
            }
            param.url = url
            var data = param || {}
            var _this=this, path=""
            data.url = url

            if(_this.isIos){
                var str = __$paramToUrlEncodeStr(data)
                 path = "ehtsec://func?id=10161&"+str
             }else{
                 data.key_url = url;
                 data.key_title=param.title;
                 var str = __$paramToUrlEncodeStr(data)
                 path = "ehtsec://func?id=10161"+"&params="+encodeURIComponent(str)
             }
            if(getPath){
             return path
             }else{
                 window.location.href = path
             }
        },
    addTask:function(params,callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('addTask', params,callBackFunction);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.appData.addTask(str)
            }
        }
    },
    finishTask:function(params,callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('finishTask', params,callBackFunction);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.appData.finishTask(str)
            }
        }
    },
    htShowGoodRating:function(params,callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('htShowGoodRating', params,callBackFunction);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.appData.htShowGoodRating(str)
            }
        }
    },
    pushNotificationGetPermission:function(callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('pushNotificationGetPermission', callBackFunction);
            })
        }else{
            if(window.appData){
                let obj = window.appData.pushNotificationGetPermission()
                callBackFunction(obj)
            }
        }
    },
    wxLaunchMiniProgram:function(params){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('wxLaunchMiniProgram', params);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.appData.wxLaunchMiniProgram(str)
            }
        }
    },
    HTVASGetALLServices:function(callBackFunction){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('HTVASGetALLServices', callBackFunction);
            })
        }else{
            if(window.appData){
                window.squareDataObj.getAccountAuth()
		//H5实现本地JS方法updateVipInfo（String params）接收回调
            }
        }
    },
    HTVASDoOpenTSGN:function(params){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('HTVASDoOpenTSGN', params);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.squareDataObj.doSquareAuth(str)
            }
        }
    },
    doOpenMap:function(params){
        var _this=this
        if(_this.isIos){
            connectWebViewJavascriptBridge(function (bridge) {
                bridge.callHandler('doOpenMap', params);
            })
        }else{
            if(window.appData){
                var str = JSON.stringify(params);
                window.squareDataObj.doOpenMap(str)
            }
        }
    },
    }
    function __$paramToUrlEncodeStr(obj){
        let strArr = []
        for(var i in obj ){
            if(obj[i]){
                let str = encodeURIComponent(obj[i])
                strArr.push(i+'='+str);
            }

        }

        return strArr.join("&")
    }
    if (typeof exports == "object") {
		module.exports = HTBridge;
	} else if (typeof define == "function" && define.amd) {
		define([], function () {
			return HTBridge;
		})
	} else {
		window.HTBridge = HTBridge;
	}

    export  {
      connectWebViewJavascriptBridge,
      HTBridge
    }
